package com.enjoyread.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.enjoyread.common.utils.BeanUtil;
import com.enjoyread.common.utils.RUtils;
import com.enjoyread.common.utils.StringUtils;
import com.enjoyread.common.vo.R;
import com.enjoyread.core.dao.UserDetailDao;
import com.enjoyread.core.dto.UserDetailUpdateDto;
import com.enjoyread.core.entity.UserDetail;
import com.enjoyread.core.service.intf.UserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: JC
 * @time: 2021/10/27 16:56
 * @description:
 */
@Service
public class UserDetailServiceImpl implements UserDetailService {
    @Autowired
    private UserDetailDao dao;
    @Override
    public R updateHead(int uid, String headurl) {
        //校验参数
        if(StringUtils.isNotEmpty(headurl)){
            //修改
            if(dao.updateHead(headurl, uid)>0){
                return RUtils.ok("ok",null);
            }
        }
        return RUtils.fail("错误");
    }

    @Override
    public R update(int uid, UserDetailUpdateDto dto) {
        //1.校验
        if(dto!=null) {
            //2.属性复制，
            UserDetail detail = BeanUtil.copyProperty(UserDetail.class, dto, dto.getClass().getDeclaredFields());
            detail.setUid(uid);
            //3.修改
            if (dao.updateById(detail) > 0) {
                return RUtils.ok("ok",null);
            }
        }
        return RUtils.fail("错误");
    }

    @Override
    public UserDetail queryByUid(int uid) {
        //1.查询条件构造器
        QueryWrapper<UserDetail> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        return dao.selectOne(queryWrapper);
    }
}
